﻿using Gym.Application.Common.Interfaces;
using Gym.Domain.RoomAggregate;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gym.Infrastructure.Persistence.Repositories
{
    public class RoomsRepository : IRoomsRepository
    {
        private readonly GymDbContext _dbContext;
        public RoomsRepository(GymDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        public async Task AddRoomAsync(Room room)
        {
            await _dbContext.Rooms.AddAsync(room);
            await _dbContext.SaveChangesAsync();
        }

        public async Task<Room?> GetByIdAsync(Guid id)
        {
            return await _dbContext.Rooms.FirstOrDefaultAsync(r => r.Id == id);
        }

        public async Task<List<Room>> ListByGymIdAsync(Guid gymId)
        {
            return await _dbContext.Rooms
                .AsNoTracking()
                .Where(r => r.GymId == gymId)
                .ToListAsync();
        }

        public async Task RemoveAsync(Room room)
        {
            _dbContext.Rooms.Remove(room);
            await _dbContext.SaveChangesAsync();
        }

        public async Task UpdateAsync(Room room)
        {
            _dbContext.Update(room);
            await _dbContext.SaveChangesAsync();
        }
    }
}
